﻿using MaterialDesignThemes.Wpf;
using Restaurant.Model;
using Restaurant.Model.ExModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Restaurant.Client.Controls
{
    /// <summary>
    /// u_Food.xaml 的交互逻辑
    /// </summary>
    public partial class u_FoodType : UserControl
    {

        public delegate void ThisClick(u_FoodType sender);
        public event ThisClick Edit;
        public event ThisClick Delete;

        private bool _isAdd = false;

        /// <summary>
        /// 是否为添加
        /// </summary>
        private bool IsAdd
        {
            get => _isAdd;
            set
            {
                _isAdd = value;
                if (_isAdd)
                    tb_Title.Text = "添加菜品";
                else
                    tb_Title.Text = "修改菜品";
            }
        }

        private FoodType_Food_Ex _foodType_Food_Ex;
        public FoodType_Food_Ex FoodType_Food_Ex
        {
            get => _foodType_Food_Ex;

            set
            {
                _foodType_Food_Ex = value;
                tb_FoodType.Text = FoodType_Food_Ex.FoodTypeName;
                tb_Count.Text = FoodType_Food_Ex.ListFood.Count.ToString();
            }
        }

        public u_FoodType()
        {
            InitializeComponent();
        }
        public u_FoodType(FoodType_Food_Ex foodType_Food_Ex) : this()

        {
            FoodType_Food_Ex = foodType_Food_Ex; 
        }

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Details_Click(object sender, RoutedEventArgs e)
        {

            sp_Main.Children.Clear();
            foreach (var item in FoodType_Food_Ex.ListFood)
            {
                var i = new u_FoodItem(item);
                i.Delete += Item_Delete;
                i.Edit += Item_Edit;
                sp_Main.Children.Add(i);
            }
           

        }

        /// <summary>
        /// 弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void dh_FoodType_DialogClosing(object sender, MaterialDesignThemes.Wpf.DialogClosingEventArgs eventArgs)
        {
            try
            {
                //取消判断
                if (!Equals(eventArgs.Parameter, true)) return;

                //保存数据 界面显示

                if (IsAdd)//添加
                {
                    var item = new u_FoodItem();
                    item.Delete += Item_Delete;
                    item.Edit += Item_Edit;
                    sp_Main.Children.Add(item);
                }
                else//保存
                {

                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                txt_Add_Number.Text = "";
                txt_Add_Remarks.Text = "";
                eventArgs.Handled = true;
            }
        }

        /// <summary>
        /// 子元素编辑
        /// </summary>
        /// <param name="sender"></param>
        private void Item_Edit(u_FoodItem sender)
        {
            IsAdd = false;
            txt_Add_Number.Text = "修改";
            txt_Add_Remarks.Text = "修改";
            DialogHost.OpenDialogCommand.Execute(null, null);
            //dh_FoodType.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 子元素 删除
        /// </summary>
        /// <param name="sender"></param>
        private void Item_Delete(u_FoodItem sender)
        {
            //删除数据

            //删除显示
            sp_Main.Children.Remove(sender);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Item_Add_Click(object sender, RoutedEventArgs e)
        {
            IsAdd = true;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Type_Delete_Click(object sender, RoutedEventArgs e)
        {
            Delete?.Invoke(this);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Type_Edit_Click(object sender, RoutedEventArgs e)
        {

            Edit?.Invoke(this);
        }


      
    }
}
